package com.moleq.network;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

public class CommTaskParser {

	public final static String START_TAG_NAME = "CommTask";
	public final static String TAG_CREATE_DATETIME = "createDatetime";
	public final static String TAG_IPADDRESS = "ipAddress";

	public InputStream readXml(String fileName) throws FileNotFoundException {

		File file = new File(fileName);
		InputStream inputStream = new FileInputStream(file);
		return inputStream;
	}

	public CommTask getCommTasks(String fileName) throws Throwable {
		CommTask commtask = new CommTask();
		StoreSetting mStoreSetting = null;
		InputStream inputStream = null;

		inputStream = readXml(fileName);

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");

		int eventType = parser.getEventType();
		List<String> dbActions = new ArrayList<String>();
		List<String> extraFiles = new ArrayList<String>();
		List<StoreSetting> companyInfolist = new ArrayList<StoreSetting>();
		
		while (eventType != XmlPullParser.END_DOCUMENT) {

			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				break;
			case XmlPullParser.START_TAG:
				String name = parser.getName();
				if ("CommTask".equals(name)) {
					commtask = new CommTask();
//					commtask.setId(parser.getAttributeValue(0));
				}
				if (commtask != null) {
					if ("createDatetime".equals(name)) {
						commtask.setCreateDateTime(parser.nextText());
					}
//					if ("station".equals(name)){
//						commtask.setStation(Integer.valueOf(parser.nextText()));
//					}
					if ("ipAddress".equals(name)) {
						commtask.setIpAddress(parser.nextText());
					}
					if ("type".equals(name)) {
						commtask.setType(parser.nextText());
					}
					
//					if ("Table".equals(name)) {
//						commtask.setTableName(parser.getAttributeValue(null, "name"));
//					}
					if("Data".equals(name)){
//						DatabaseAction da = new DatabaseAction(
//								parser.getAttributeValue(null, "type"),
//								commtask.getTableName(),
//								parser.nextText()
//								);
						dbActions.add(parser.nextText());
					}
					if("Extrafile".equals(name)){
						
						extraFiles.add(parser.nextText());
					}
					
					if ("RegCode".equals(name)) {
						commtask.setRegCode(parser.nextText());
						
					}
					
					if ("CompanyInfo".equals(name)) {
						mStoreSetting = new StoreSetting();
						
					}
					
					if (mStoreSetting != null) {
						//////////RegCode
						
						if ("Name".equals(name)) {
							mStoreSetting.setSs_name(parser.nextText());
						}
						
						if ("OtherName".equals(name)) {
							mStoreSetting.setSs_other_name(parser.nextText());
						}
						
						if ("Address1".equals(name)) {
							mStoreSetting.setSs_address_1(parser.nextText());
						}
						
						if ("Address2".equals(name)) {
							mStoreSetting.setSs_address_2(parser.nextText());
						}
						
						if ("City".equals(name)) {
							mStoreSetting.setSs_city(parser.nextText());
						}
						
						if ("State".equals(name)) {
							mStoreSetting.setSs_state(parser.nextText());
						}
						
						if ("Zip".equals(name)) {
							mStoreSetting.setSs_zip(parser.nextText());
						}
						
						if ("Telephone".equals(name)) {
							mStoreSetting.setSs_telephone(parser.nextText());
						}
						
						
						if ("Fax".equals(name)) {
							mStoreSetting.setSs_fax(parser.nextText());
						}
						
						
						if ("Website".equals(name)) {
							mStoreSetting.setSs_website(parser.nextText());
						}
						
						if ("BusinessName".equals(name)) {
							mStoreSetting.setSs_business_name(parser.nextText());
						}
												
						companyInfolist.add(mStoreSetting);
					}
					
				}
				
				break;
			case XmlPullParser.END_TAG:
//				if ("CommTask".equals(parser.getName())) {
//					tasks.add(commtask);
//					commtask = null;
//				}
				break;
			default:
				break;
			}
			eventType = parser.next();
		}
		
		commtask.setDbAction(dbActions);
		commtask.setExtraFiles(extraFiles);
		commtask.setCompanyInfo(companyInfolist);
		
		if (inputStream != null)
			inputStream.close();

		return commtask;

	}

	public void save(CommTask tasks, String fileName) throws Throwable {

		File newfile = new File(fileName);
		if (newfile.isDirectory())
			return;

		if (!newfile.exists()) {
			newfile.createNewFile();
		}

		FileWriter writer = new FileWriter(new File(fileName));

		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(writer);
		serializer.startDocument("UTF-8", true);
		serializer.startTag(null, "CommTask");
		//create time
		serializer.startTag(null,"createDatetime");
		serializer.text(tasks.getCreateDateTime());
		serializer.endTag(null,"createDatetime");
		
		// station
//		serializer.startTag(null,"station");
//		serializer.text(String.valueOf(tasks.getStation()));
//		serializer.endTag(null,"station");
		
		// ip address
		serializer.startTag(null,"ipAddress");
		serializer.text(tasks.getIpAddress());
		serializer.endTag(null,"ipAddress");
		
		// type
		serializer.startTag(null,"type");
		serializer.text(tasks.getType());
		serializer.endTag(null,"type");
		
//		serializer.startTag(null,"Action");
		
//		// db structure
//		serializer.startTag(null,"Table");
//		serializer.attribute(null, "name", tasks.getTableName());
//		serializer.endTag(null,"Table");
		
		
		serializer.startTag(null,"Records");
		
		// data
		List<String> lst = tasks.getDbAction();
		for(int i = 0;i < lst.size();i++){
			serializer.startTag(null,"Data");
			serializer.text( lst.get(i));
			serializer.endTag(null,"Data");
		}
		
		serializer.endTag(null,"Records");
		
		
		// extra file
		serializer.startTag(null,"ExtraFiles");
		
		// data
		List<String> lst2 = tasks.getExtraFiles();
		for(int i = 0;i < lst2.size();i++){
			serializer.startTag(null,"Extrafile");
			serializer.text( lst2.get(i));
			serializer.endTag(null,"Extrafile");
		}
		
		serializer.endTag(null,"ExtraFiles");
	
//		serializer.endTag(null,"Action");
		serializer.endTag(null, "CommTask");
		serializer.endDocument();
		
		writer.flush();
		writer.close();
	}
}
